package com.itholmes.singleton;

//饿汉式的单例设计模式：
public class SingleObject {
	private static SingleObject instance = new SingleObject();

	private SingleObject() {
		
	}
	//单例设计模式，都是private权限，包括构造器，仅有一个方法可以获取到一个对象
	public static SingleObject getInstance() {
		return instance;
	}
}

//懒汉式的单例设计模式：
class SingleObject02{
	private static SingleObject02 instance;
	
	private SingleObject02() {}
	
	//在方法内部声明对象(synchronized是线程安全)
	public static synchronized SingleObject02 getInstance() {
		//要判断instance是否为null
		if(instance == null) {
			instance = new SingleObject02();
		}
		return instance;
	}
	
}



